package dyyx;

import fi.iki.elonen.NanoHTTPD;
import fi.iki.elonen.NanoHTTPD.IHTTPSession;

public class SimpleHttpServer extends NanoHTTPD {
	
	private final int port;
	private final WebServerHandler handler;

	
	public SimpleHttpServer(int port,WebServerHandler handler) throws Exception {
		super(port);
		this.port = port;
		this.handler = handler; 
		this.start();
	}
	
	@Override
    public Response serve(IHTTPSession session) {	
		// parse post 数据
		Response res = super.serve(session);
		// Map<String, String> params = session.getParms();	
		System.out.println(session.getMethod()+","+session.getUri()+","+session.getQueryParameterString()+","+session.getParms());		
		WebServerContext ctx = new WebServerContext(session);				
		handler.run(ctx);	
		return newFixedLengthResponse(ctx.response);
	}
}
